//
//  GMAnalysisCell.m
//  Teecloud
//
//  Created by 小熊 on 2018/11/15.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMAnalysisCell.h"
#import "GMSubjectsModel.h"

#import "GMExeTabHeadCell.h"
#import "GMSubjectsItem.h"

#import "GMAnalysisTabCell.h"
#import "GMAnserCell.h"
#import "GMExplainCell.h"
@interface GMAnalysisCell()<UITableViewDataSource,UITableViewDelegate>
/**用来显示组合题的内容*/
@property (weak, nonatomic) IBOutlet UITextView *textView;
/**组合题内容的高度，如果没有内容，高度等于0，默认250*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHight;
/**中间凸起的图片,当组合题有值，才显示这个图片*/
@property (weak, nonatomic) IBOutlet UIImageView *tuqImg;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tuqHight;
/**用来显示小题的内容*/
@property (weak, nonatomic) IBOutlet UITableView *tabView;
/**习题名字*/
@property (weak, nonatomic) IBOutlet UILabel *titleName;
/**当前习题*/
@property (weak, nonatomic) IBOutlet UILabel *curentCount;
/**总题数*/
@property (weak, nonatomic) IBOutlet UILabel *totalCount;
/**选项卡数组*/
@property (nonatomic, strong) NSMutableArray *tabData;
/**当前时间,  本题开始时间【必须是 yyyy-MM-dd HH:mm:ss 格式】*/
@end
@implementation GMAnalysisCell


- (void)awakeFromNib {
    [super awakeFromNib];
    /**设置textView不可编辑*/
    [self.textView setEditable:NO];
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    [self.tabView registerNib:[UINib nibWithNibName:@"GMExeTabHeadCell" bundle:nil] forCellReuseIdentifier:@"GMExeTabHeadCell"];
    [self.tabView registerNib:[UINib nibWithNibName:@"GMAnalysisTabCell" bundle:nil] forCellReuseIdentifier:@"GMAnalysisTabCell"];
    [self.tabView registerNib:[UINib nibWithNibName:@"GMAnserCell" bundle:nil] forCellReuseIdentifier:@"GMAnserCell"];
    [self.tabView registerNib:[UINib nibWithNibName:@"GMExplainCell" bundle:nil] forCellReuseIdentifier:@"GMExplainCell"];
   
}
-(NSMutableArray *)tabData{
    if (!_tabData) {
        _tabData = [[NSMutableArray alloc] init];
    }
    return _tabData;
}
-(void)setName:(NSString *)name{
    _name = name;
    self.titleName.text = self.name;
}
-(void)setIndex:(NSInteger)index{
    _index = index;
    self.curentCount.text = [NSString stringWithFormat:@"%ld",index];
}
-(void)setTotal:(NSInteger)total{
    _total = total;
    self.totalCount.text = [NSString stringWithFormat:@"/%ld",total];
}

-(void)setModel:(GMSubjectsModel *)model{
    _model = model;
    if (kIsEmptyString(model.GroupContent)) {
        self.textView.hidden = YES;
        self.textViewHight.constant = 0;
        self.tuqImg.hidden = YES;
        self.tuqHight.constant =0;
    }else{
        self.textView.hidden = NO;
        self.textViewHight.constant = 250;
        self.tuqImg.hidden = NO;
        self.tuqHight.constant =30;
        NSMutableAttributedString *str=  [[NSMutableAttributedString alloc] initWithData:[model.GroupContent dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        [str addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, str.length)];
        self.textView.attributedText = str;
    }
    [self.tabData removeAllObjects];
    [self.tabData addObjectsFromArray:model.Items];
    /**在这里操纵数据源，用于控制答案的选中状态*/
    //将字符串整体作为分割条件 返回值为NSArray不可变数组
    NSArray * array = [model.Answer componentsSeparatedByString:@","];
    for (NSInteger i=0; i<array.count; i++) {
        NSString *temStr = array[i];
        for (NSInteger j =0; j<self.tabData.count; j++) {
            if ([temStr isEqualToString:@"A"]) {
                GMSubjectsItem *itemModel = self.tabData[0];
                itemModel.selected = YES;
                itemModel.selctedAnswer = temStr;
            }else if ([temStr isEqualToString:@"B"]){
                GMSubjectsItem *itemModel = self.tabData[1];
                  itemModel.selected = YES;
                itemModel.selctedAnswer = temStr;
            }else if ([temStr isEqualToString:@"C"]){
                GMSubjectsItem *itemModel = self.tabData[2];
                  itemModel.selected = YES;
                itemModel.selctedAnswer = temStr;
            }else if ([temStr isEqualToString:@"D"]){
                GMSubjectsItem *itemModel = self.tabData[3];
                  itemModel.selected = YES;
                itemModel.selctedAnswer = temStr;
            }else if ([temStr isEqualToString:@"E"]){
                GMSubjectsItem *itemModel = self.tabData[4];
                  itemModel.selected = YES;
                itemModel.selctedAnswer = temStr;
            }else if ([temStr isEqualToString:@"F"]){
                GMSubjectsItem *itemModel = self.tabData[5];
                  itemModel.selected = YES;
                itemModel.selctedAnswer = temStr;
            }else if ([temStr isEqualToString:@"G"]){
                GMSubjectsItem *itemModel = self.tabData[6];
                  itemModel.selected = YES;
                itemModel.selctedAnswer = temStr;
            }
        }
    }
    
    NSArray *tempArray = [model.StandardAnswer componentsSeparatedByString:@","];
    for (NSInteger i=0; i<tempArray .count; i++) {
        NSString *temStr = tempArray [i];
        for (NSInteger j =0; j<self.tabData.count; j++) {
            GMSubjectsItem *itemModel = self.tabData[j];
            if ([temStr isEqualToString:itemModel.selctedAnswer]) {
                //如果选择答案等于标准答案
                itemModel.IsCorrect = YES;
            }
        }
    }
   [self.tabView reloadData];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tabData.count +3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        GMExeTabHeadCell *headCell = [tableView dequeueReusableCellWithIdentifier:@"GMExeTabHeadCell" forIndexPath:indexPath];
        headCell.model = self.model;
        return headCell;
    }else if(indexPath.row ==self.tabData.count+1){
        GMAnserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMAnserCell" forIndexPath:indexPath];
        cell.model = self.model;
        return cell;
    }else if(indexPath.row ==self.tabData.count+2){
        GMExplainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMExplainCell" forIndexPath:indexPath];
        cell.model = self.model;
        return cell;
    }else{
        GMAnalysisTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMAnalysisTabCell" forIndexPath:indexPath];
        GMSubjectsItem *itemModel =self.tabData[indexPath.row-1];
        cell.model = itemModel;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return self.model.titleHight;
    }else if(indexPath.row ==self.tabData.count+1){
        return 100;
    }else if(indexPath.row ==self.tabData.count+2){
         return self.model.explanHight;
    }else{
        GMSubjectsItem *itemModel =self.tabData[indexPath.row-1];
        return itemModel.cellHight;
    }
}
@end
